The original figure is from Five Thirty Eight’s article “Are White Evangelicals Sacrificing The Future In Search Of The Past?” posted January 24, 2018.
The original data was retrieved from the Public Religion Research Institute (PRRI) and included in the organizations’ report “America’s Changing Religious Identity” from September 6, 2017. PRRI provided a figure of their own this this data. The data is availible for download as a .csv file.
library(plotly)## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.5.2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(knitr)## Warning: package 'knitr' was built under R version 3.5.2
library(tidyverse)## ── Attaching packages ───────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ tibble 2.1.1 ✔ purrr 0.3.2
## ✔ tidyr 0.8.3 ✔ dplyr 0.8.0.1
## ✔ readr 1.3.1 ✔ stringr 1.4.0
## ✔ tibble 2.1.1 ✔ forcats 0.4.0
## Warning: package 'tibble' was built under R version 3.5.2
## Warning: package 'tidyr' was built under R version 3.5.2
## Warning: package 'purrr' was built under R version 3.5.2
## Warning: package 'dplyr' was built under R version 3.5.2
## Warning: package 'stringr' was built under R version 3.5.2
## Warning: package 'forcats' was built under R version 3.5.2
## ── Conflicts ──────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks plotly::filter(), stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(ggplot2)
library(ggthemes)## Warning: package 'ggthemes' was built under R version 3.5.2
library(RColorBrewer)
library(DT)## Warning: package 'DT' was built under R version 3.5.2
#rename data set
religion_data = read_csv("./Raw Data/a-generational-shift-in.csv")## Parsed with column specification:
## cols(
## `Source: PRRI 2016 American Values Atlas.` = col_character(),
## `Don't know/Refused` = col_double(),
## Unaffiliated = col_double(),
## `Other religion` = col_double(),
## `Other world religions` = col_double(),
## Jewish = col_double(),
## `Other Christian` = col_double(),
## `Hispanic Catholic` = col_double(),
## `Hispanic Protestant` = col_double(),
## `Black Protestant` = col_double(),
## Mormon = col_double(),
## `White Catholic` = col_double(),
## `White mainline Protestant` = col_double(),
## `White evangelical Protestant` = col_double()
## )
kable(religion_data)| Source: PRRI 2016 American Values Atlas. | Don’t know/Refused | Unaffiliated | Other religion | Other world religions | Jewish | Other Christian | Hispanic Catholic | Hispanic Protestant | Black Protestant | Mormon | White Catholic | White mainline Protestant | White evangelical Protestant |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 18-29 | 3 | 38 | 1 | 4 | 1 | 7 | 9 | 6 | 7 | 2 | 6 | 8 | 8 |
| 30-49 | 3 | 26 | 1 | 3 | 1 | 6 | 10 | 5 | 8 | 2 | 9 | 11 | 14 |
| 50-64 | 3 | 18 | 1 | 1 | 2 | 6 | 6 | 2 | 9 | 2 | 15 | 15 | 21 |
| 65+ | 3 | 12 | 1 | 1 | 2 | 5 | 4 | 1 | 7 | 2 | 16 | 19 | 26 |
#rename age column
names(religion_data)[1]<-"age_group"
#the original data set is not tidy. The variables are: religion and age. There are counts or percentages for each group.
religion_data_tidy = religion_data %>% gather(key = "religion", value = "percent", -`age_group`)
datatable(religion_data_tidy, options = list(pageLength = 5))The figure from Five Thirty Eight is a bar plot that shares the religious identitity of respondants as a percentage of total respondants questions (proportions). The responses are binned by age group and colored by religious identity.
religion_data_tidy %>%
ggplot(aes(fill= religion, y= percent, x=age_group)) +
geom_bar(position="fill", stat="identity")religion_data_tidy %>%
ggplot(aes(fill= factor(religion, levels=unique(religion)), y= percent, x=age_group)) +
geom_bar(position="fill", stat="identity", width = 0.25) +
xlab("Age") +
ggtitle("A generational shift in religious identity", subtitle = "Share of respondants by age group and religious affiliation") +
scale_x_discrete(position = "top")religion_data_tidy %>%
ggplot(aes(fill= factor(religion, levels=unique(religion)), y= percent, x=age_group)) +
geom_bar(position="fill", stat="identity", width = 0.25) +
xlab("Age") +
ggtitle("A generational shift in religious identity", subtitle = "Share of respondants by age group and religious affiliation") +
scale_x_discrete(position = "top")+
theme(axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
guides(fill=guide_legend(title=NULL))religion_data_tidy %>%
ggplot(aes(fill= factor(religion, levels=unique(religion)), y= percent, x=age_group)) +
geom_bar(position="fill", stat="identity", width = 0.25) +
xlab("Age") +
ggtitle("A generational shift in religious identity", subtitle = "Share of respondants by age group and religious affiliation") +
scale_x_discrete(position = "top")+
theme(axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
guides(fill=guide_legend(title=NULL)) +
scale_fill_manual(values=c("snow3","darkolivegreen4", "darkolivegreen3","darkolivegreen1", "darkolivegreen2", "plum3", "plum2", "plum4", "plum1","coral3","khaki1", "chocolate2", "chocolate3")) #To build this figure, we need to build upon base ggplot() and add the geom_bar() function.
#We need to figure out how to order the "fills" by religion. The order in the original figure seems to have been alphabetized by ggplot automatically. How do we undo this? - Set religion as a factor.
#COMPLETED
#Load data
#Generate plot
#Reorder the fills
#Add chart labels
#Create a minimal theme by removing all y labels and axes
#Moved X axis to top of chart
#TO-DO
#Find a way to change legend into labels
#Find a way to add data labels inside of the stacks
#Move the "Don't Know/Refused" to the bottom
religion_data_tidy %>%
ggplot(aes(fill= factor(religion, levels=unique(religion)), y= percent, x=age_group)) +
geom_bar(position="fill", stat="identity", width = 0.25) +
xlab("Age") +
ggtitle("A generational shift in religious identity", subtitle = "Share of respondants by age group and religious affiliation") +
scale_x_discrete(position = "top")+
theme(axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
guides(fill=guide_legend(title=NULL)) +
scale_fill_manual(values=c("snow3","darkolivegreen4", "darkolivegreen3","darkolivegreen1", "darkolivegreen2", "plum3", "plum2", "plum4", "plum1","coral3","khaki1", "chocolate2", "chocolate3")) Tried and Failed
Adding border around “White evangelical Protestant” - I attempted to add a border by adding an ifelse statement in the geom_bar() function. This included geom_bar(position=“fill”, stat=“identity”, width = 0.25, color = ifelse(religion == “White evangelical Protestant”), “black”, FALSE). This did not work. The religion variable was not identified. The FALSE argument is not possible here, according to the errors (though I have seen color = FALSE used elsewhere)
Chaning the Color Pallete - I had a hard time finding this figure’s color pallete or a form of gradient that would work. I went through and hand-picked the colors.
Changing Legend - I tried to line up the Legend with the sections of the 65+ bar, as seen in the original figure. I tried this by different variations of geom_text(). One configuration was: geom_text(x == “65+”,label~religion). Did not work here.
Adding data labels - There are a few complications with this. The number labels are both black and white.